package actions;

import java.io.File;
import java.io.FileOutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
import dto.JfMembers;
import beans.RegisterationFormBean;
import client.UserModel;
import java.util.Date;
public class RegisterationAction extends org.apache.struts.action.Action {


    public RegisterationAction()
    {
    	 System.out.println("I am in action");
    }

    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        System.out.println("I am in action");
        ActionForward forward=new ActionForward();
      //============================================================
        String username=((RegisterationFormBean)form).getUserName();
        String password=((RegisterationFormBean)form).getPassword();
        String fname=((RegisterationFormBean)form).getFname();
        String lname=((RegisterationFormBean)form).getLname();
        String radio=((RegisterationFormBean)form).getRadio();
        String country=((RegisterationFormBean)form).getCountry();
        String mail=((RegisterationFormBean)form).getMail();
        String cfpw=((RegisterationFormBean)form).getConfirmpassword();
        System.out.println("In action "+cfpw);
        //========================================================
         if(!password.equals(cfpw)) forward=mapping.findForward("fail");
         else{
        //=======================Avatar===========================
        //======================Checking==========================
          //String newfilepath=getServlet().getServletContext().getRealPath("/")+"upload";
          File file=new File(getServlet().getServletContext().getRealPath("/")+"upload");
          boolean exist=file.exists();
          if(exist)
          {
              System.out.println("Exists");
          }
          else
          {
              System.out.println("Not Exist");
              file.mkdir();
              System.out.println("file Created");
          
          }
        
        //========================================================
        FormFile myFile=((RegisterationFormBean)form).getTheFile();
        String contentType = myFile.getContentType();
        String fileName    = myFile.getFileName();
        byte[] fileData    = myFile.getFileData();
        String filePath=getServlet().getServletContext().getRealPath("/")+"upload"+"/"+username;
        File f = new File(filePath);
        f.mkdir();
        String imgPath=filePath+"\\"+fileName;
       if(!fileName.equals(""))
        {   System.out.println("Server path:" +filePath);
            File fileToCreate = new File(filePath, fileName);

            if(!fileToCreate.exists())
            {
              FileOutputStream fileOutStream = new FileOutputStream(fileToCreate);
              fileOutStream.write(myFile.getFileData());
              fileOutStream.flush();
              fileOutStream.close();
              System.out.println("File Uploaded");
            }
        }
         
 //===================================Filling Up jfmember obj======================================
        JfMembers mem=new JfMembers();
        mem.setUsername(username);
        mem.setPassword(password);
        mem.setFName(fname);
        mem.setLName(lname);
        mem.setGender(radio);
        mem.setCountry(country);
        mem.setEMail(mail);
        mem.setBanned(0);
        mem.setUnsuspenddate((new Date())); ////////////////////////////////////////////
        mem.setUserprev(0);
        
        
        mem.setAvatar("upload"+"/"+username+"/"+fileName);
   //======================================create trial obj of session==============================
         HttpSession session=request.getSession(false);
         session.setAttribute("userBean", mem);
        System.out.println("session id is"+ session.getId());
         
  //======================================ready to register=========================================
        UserModel model=new UserModel();
       boolean ret= model.addNewUser(mem);
       if(ret)
       {
    	   forward=mapping.findForward("success");
       }
       else
       {
    	   System.out.print("back");
    	   forward=mapping.findForward("fail");
       }
         }
    	return forward;
    }

}